package com.portal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.portal.constant.RedisConst;
import com.portal.model.entity.Book;
import com.portal.service.BookService;
import com.portal.mapper.BookMapper;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Service;

/**
* @author 61073
* @description 针对表【t_book】的数据库操作Service实现
* @createDate 2025-09-27 09:54:25
*/
@Service
public class BookServiceImpl
        extends ServiceImpl<BookMapper, Book>
    implements BookService{

    // 热销榜：分页、根据销量降序排序
    @Override
    @Cacheable(cacheNames = RedisConst.BOOK_LIST,keyGenerator = "keyGenerator")
    public Page<Book> findTopSales(Integer pageNum, Integer pageSize) {
        // 分页对象：当前页、页大小
        Page<Book> page = new Page<>(pageNum, pageSize);
        // 构造条件
        LambdaQueryWrapper<Book> wrapper = Wrappers.lambdaQuery(Book.class);
        wrapper.orderByDesc(Book::getBuyCount);
        // 分页查询
        Page<Book> pageData = this.page(page, wrapper);
        return pageData;
    }

    @Cacheable(cacheNames = RedisConst.BOOK_LIST,keyGenerator = "keyGenerator")
    @Override
    public IPage<Book> findByType(Integer pageNum, Integer pageSize, Integer typeId) {
        // 封装分页参数
        IPage<Book> iPage = new Page<>(pageNum, pageSize);
        // 条件查询
        IPage<Book> pageData = this.page(iPage,
                Wrappers.lambdaQuery(Book.class)
                        .eq(Book::getTypeId, typeId)
                        .orderByDesc(Book::getUpdateTime)
        );
        return pageData;
    }

    @Cacheable(cacheNames = RedisConst.BOOK_DETAIL, key = "#root.methodName + ':' + #p0")
    @Override
    public Book findById(Long id) {
        return this.getById(id);
    }


}




